from game import zz_game
from zz import zz_log, zz_img

log = zz_log.of()


class Game(zz_game.ZZ_Game):
    def __init__(self, d):
        super().__init__(d)

    def get_name(self):
        return "约战沙城"

    def check_done(self):
        return False

    def swipe_up(self):
        """
        向上滑半屏
        :return:
        """
        self.log_msg("向上滑")
        w, h = self.d.window_size()
        self.d.swipe(w / 2, h / 2, w / 2, h * 0.1, 0.5)

    def click_close(self):
        self.log_msg("点击关闭")
        self.img_click("./game/img/约战沙城/exit.png")

    def task_mall(self):
        """
        商城任务
        """
        self.img_click_if_exists("点击日常活动", "./game/img/约战沙城/play.png", './game/img/约战沙城/play_main.png')

        if not self.img_exit('./game/img/约战沙城/mall.png'):
            self.log_msg('商城任务，没找到，估计已完成')
            self.click_close()
            return False

        self.task_click('前行商城任务', './game/img/约战沙城/mall.png')

        self.log_msg("点击人参")
        self.img_click("./game/img/约战沙城/mall01.png")
        self.log_msg("点击购买")
        self.img_click("./game/img/约战沙城/mall02.png")
        self.click_close()

        return True

    def task_play(self):
        """
        刷怪任务
        """
        self.img_click_if_exists("点击日常活动", "./game/img/约战沙城/play.png", './game/img/约战沙城/play_main.png')
        exit_play = False
        for i in range(2):
            if self.img_exit('./game/img/约战沙城/play_room.png'):
                exit_play = True
                break
            self.swipe_up()

        if not exit_play:
            self.log_msg('刷怪任务，没找到，估计已完成')
            self.click_close()
            return False

        self.task_click('前行刷怪任务', './game/img/约战沙城/play_room.png')
        self.log_msg("等待进入刷怪任务列表")
        self.img_exit("./game/img/约战沙城/play_room_main.png", 20)
        self.log_msg("已进入刷怪任务列表")

        for i in range(2):
            ok = self.task_play_room()
            if not ok:
                break

        self.log_msg("点击遗落禁地")
        self.img_click("./game/img/约战沙城/play_room_01.png", 3)
        for i in range(2):
            ok = self.task_play_room()
            if not ok:
                break
        self.log_msg("点击关闭")
        self.img_click("./game/img/约战沙城/exit.png")

    def task_play_room(self):
        """
        挑战副本
        """
        self.log_msg("点击前往挑战")
        self.img_click("./game/img/约战沙城/play_room_go.png", 3)
        self.log_msg("等待进入挑战界面")
        if not self.img_exit("./game/img/约战沙城/play_room_play_main.png", 8):
            self.log_msg("挑战机会已用完")
            return False
        self.log_msg("已进入挑战界面")
        while True:
            if self.img_exit("./game/img/约战沙城/play_room_done.png", 2):
                break
            self.log_msg("刷怪中")
            self.d.sleep(2)

        self.log_msg("挑战完成，点击领取")
        self.img_click("./game/img/约战沙城/play_room_done.png", 3)
        return True

    def task_click(self, task_msg, target_img):
        """
        点击任务前往
        """
        r = self.find_img(target_img)
        if r.err():
            self.log_msg(f"{task_msg}不存在")
            return False
        x, y = r.get_data("first")
        self.log_msg("点击任务")
        self.d.click(x + 700, y)
        return True

    def handle_main(self):
        self.log_msg("等待进入游戏页面")
        self.img_exit("./game/img/约战沙城/start.png", 20)
        self.log_msg("点击开始游戏")
        self.img_click("./game/img/约战沙城/start.png")
        self.log_msg("点击开始游戏")
        self.img_click("./game/img/约战沙城/start01.png")
        self.log_msg("等待进入游戏主界面")
        self.img_exit("./game/img/约战沙城/play.png", 20)
        self.log_msg("已进入游戏主界面")
        self.d.sleep(2)

        self.task_mall()  # 买人参
        self.task_play()  # 刷怪

        self.log_msg("领取奖励，点击活动")
        self.img_click("./game/img/约战沙城/play.png")
        while self.img_click("./game/img/约战沙城/get.png"):
            self.log_msg("点击领取")
        self.log_msg("领取完成")

        return True
